

clear all
set more off
capture log close
set mat 5000

cd "C:\Users\rujia\Dropbox\coalmine\restat"
use  CoalMine.dta, clear
drop if native==.
drop if leader_id==.

gen ln_output = log(prod0)

gen prod0_normalized=prod0/10000
label var prod0_normalized "Output"


label var ln_output "ln Output"


bysort prov_id: egen pre_deathrate=mean(deathrate) if year<1998
bysort prov_id: egen m_pre_deathrate=mean(pre_deathrate) 
gen pre_year=m_pre_deathrate*year
gen decentralized=(year>=1998 & year<=2000)

tabu prov_id, gen (pdummy)
forvalues i=1(1)22{
gen prov_year`i'=pdummy`i'*year
}

gen period=1 if year<1998
replace period=2 if year>=1998 & year<=2000
replace period=3 if year>2000 & year<=2005

egen clusterg=group(prov_id period)

gen decentralized_native=decentralized*native

gen ln_wage=log(indwage)
gen tenure=year-startyear+1
gen ln_gdpper=log(gdpper)
gen ln_electricity=log(eleconsum)
gen ln_disbeijing=log(1+dis_beijing)


merge 1:1 prov_id year  using TrafficPanel.dta

drop _m

merge 1:1 prov_id year using LocalCoalmine.dta

drop _m
merge 1:1 prov_id year using Population.dta



gen trafficper=mortality*10/popu



local controls "ln_output ln_wage tenure age ln_gdpper ln_electricity gov_native s_native ln_disbeijing trafficper"


foreach var in ln_output ln_wage tenure age ln_gdpper ln_electricity gov_native s_native ln_disbeijing trafficper{
gen decentralized_`var'=decentralized*`var'
}

gen native_decentralized=native*decentralized
label var native_decentralized "Decentralization * Native"
label var native "Native"

drop if year<1995
drop if year>2005

replace daily_pub=0 if daily_pub==. & year>1995
replace sat_pub=0 if sat_pub==. & year>1995
replace fri_pub=0 if fri_pub==. & year>1995
replace thurs_pub=0 if thurs_pub==. & year>1995


gen NewsCirculation=daily_pub+sat_pub+fri_pub+thurs_pub


gen NewsCirPer=NewsCirculation/(popu*10)

sum  NewsCirPer

**
replace daily_num=0 if daily_num==. & year>1995
replace sat_num=0 if sat_num==. & year>1995
replace fri_num=0 if fri_num==. & year>1995
replace thurs_num=0 if thurs_num==. & year>1995

gen NewsNumber=daily_num+sat_num+fri_num+thurs_num

sum NewsNumber
xtset prov_id year

gen lagNewsCirPer=l.NewsCirPer

gen lagNewsNumber=l.NewsNumber




gen lagNewsCirPer_decentralized=lagNewsCirPer*decentralized
gen lagNewsCirPer_native= lagNewsCirPer*native
gen lagNewsCirPer_nd= lagNewsCirPer_native*decentralized



gen  lagNewsNumber_decentralized=lagNewsNumber*decentralized
gen  lagNewsNumber_native=lagNewsNumber*native
gen lagNewsNumber_nd=lagNewsNumber_native*decentralized

local controls "ln_output ln_wage tenure age ln_gdpper ln_electricity gov_native s_native ln_disbeijing trafficper"

capture drop decentralized_native

#delimit ;

eststo clear;

eststo: xi: regress deathrate   lagNewsNumber_nd  lagNewsNumber_native lagNewsNumber_decentralized native_decentralized
native  lagNewsNumber i.prov_id i.year `controls'  decentralized_* i.prov_id i.year, cluster(clusterg);

estadd local pfe "Y";
estadd local yfe "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);

eststo: xi: regress deathrate   lagNewsNumber_nd  lagNewsNumber_native lagNewsNumber_decentralized native_decentralized 
 lagNewsNumber i.prov_id i.year `controls'  decentralized_* i.leader_id i.year, cluster(clusterg);

estadd local pfe "Y";
estadd local yfe "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd local rfe "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo:  xi: regress deathrate   lagNewsCirPer_nd  lagNewsCirPer_native lagNewsCirPer_decentralized native_decentralized
native  lagNewsCirPer i.prov_id i.year `controls'  decentralized_* i.prov_id i.year, cluster(clusterg);
estadd local pfe "Y";
estadd local yfe "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo:  xi: regress deathrate   lagNewsCirPer_nd  lagNewsCirPer_native lagNewsCirPer_decentralized native_decentralized
 lagNewsCirPer i.prov_id i.year `controls'  decentralized_* i.leader_id i.year, cluster(clusterg);

estadd local pfe "Y";
estadd local yfe "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd local rfe "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


esttab using draft/NewspaperEffect.tex, replace
	width(\hsize) 
    nomtitles
	b(3)
	se(3)
	noconstant
	star(* 0.10 ** 0.05 *** 0.01)
	keep(
lagNewsNumber_nd  
lagNewsNumber_native 
lagNewsNumber_decentralized 
lagNewsNumber
 lagNewsCirPer_nd
 lagNewsCirPer_native 
 lagNewsCirPer_decentralized
  lagNewsCirPer 
native_decentralized
native  
	)
order(
lagNewsNumber_nd  
lagNewsNumber_native 
lagNewsNumber_decentralized 
lagNewsNumber
 lagNewsCirPer_nd
 lagNewsCirPer_native 
 lagNewsCirPer_decentralized
  lagNewsCirPer 
native_decentralized
native 
	)
	
	label
	stats(
		pfe 
		yfe
		
		controls
		controls_d
         rfe
		nobs
		rsq
		, 
		labels(
			"Province FE" 
			"Year FE"
			"Controls"
			"Decentralization * Controls"
		    "Regulator FE"
			"\# observations"
			"R-Squared"
			) 
		fmt(0 0 0 0 0 0  2)
		layout(@)
		)
	nonotes
	addnotes(
	)
	;

